const router = require('koa-router')()
const {getAskMy, getAsk, getAskById, postAsk, patchAsk, deleteAsk, getAskYours} = require('../../controller/ask')

router.prefix('/api/ask')

router.get('/ask', async (ctx, next) => {
  const {status, pageIndex, pageSize} = ctx.query
  const createTime = ctx.query['createTime[]']
  ctx.body = await getAsk({status, createTime, pageIndex, pageSize})
})

router.get('/ask/my', async (ctx, next) => {
  const {status, pageIndex, pageSize} = ctx.query
  const createTime = ctx.query['createTime[]']
  ctx.body = await getAskMy(ctx, {status, createTime, pageIndex, pageSize})
})

router.get('/ask/yours', async (ctx, next) => {
  const {status, pageIndex, pageSize} = ctx.query
  const createTime = ctx.query['createTime[]']
  ctx.body = await getAskYours(ctx, {status, createTime, pageIndex, pageSize})
})

router.get('/ask/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await getAskById(id)
})

router.post('/ask', async (ctx, next) => {
  const {content, rent, startTime, endTime, houseId, status} = ctx.request.body
  ctx.body = await postAsk(ctx, {content, rent, startTime, endTime, houseId, status})
})

router.patch('/ask', async (ctx, next) => {
  const {content, rent, startTime, endTime, reason, status, id} = ctx.request.body
  ctx.body = await patchAsk({content, rent, startTime, endTime, reason, status, id})
})

router.patch('/ask/status', async (ctx, next) => {
  const {status, id} = ctx.request.body
  ctx.body = await patchAsk({status, id})
})

router.delete('/ask/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await deleteAsk(id)
})

module.exports = router
